
package View;

import Control.ControlOwner;
import Control.ControlPet;
import Control.ControlVeterinarian;
import Entity.Owner;
import Entity.Pet;
import Entity.Veterinarian;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

public class MainPanel extends javax.swing.JPanel {

    private ControlPet cp;
    private ControlOwner co;
    private ControlVeterinarian cv;
    public String target=null;
    
    public MainPanel() {
        initComponents();
        Thread t = new Thread((Clock)getDate_text());
        t.start(); 
        cp = new ControlPet();
        co = new ControlOwner();
        cv = new ControlVeterinarian();
        repaintOwnerTable();
        repaintPetTable();
        repaintVeterinarianTable();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        date_text = new Clock();
        jScrollPane1 = new javax.swing.JScrollPane();
        veterinarian_table = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        owner_table = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        pet_table = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(465, 284));
        setMinimumSize(new java.awt.Dimension(465, 284));
        setPreferredSize(new java.awt.Dimension(465, 284));

        jLabel1.setFont(new java.awt.Font("Tw Cen MT Condensed", 0, 36)); // NOI18N
        jLabel1.setText("HPVAS");

        date_text.setText("initializing...");

        jScrollPane1.setMaximumSize(new java.awt.Dimension(155, 32767));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(155, 23));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(155, 100));

        veterinarian_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Veterinarian", "ID"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        veterinarian_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                veterinarian_tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(veterinarian_table);
        veterinarian_table.getColumnModel().getColumn(0).setResizable(false);
        veterinarian_table.getColumnModel().getColumn(1).setResizable(false);

        jScrollPane2.setMaximumSize(new java.awt.Dimension(155, 32767));
        jScrollPane2.setMinimumSize(new java.awt.Dimension(155, 23));
        jScrollPane2.setPreferredSize(new java.awt.Dimension(155, 100));

        owner_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Owner", "ID"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        owner_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                owner_tableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(owner_table);
        owner_table.getColumnModel().getColumn(0).setResizable(false);
        owner_table.getColumnModel().getColumn(1).setResizable(false);

        jScrollPane3.setMaximumSize(new java.awt.Dimension(155, 32767));
        jScrollPane3.setMinimumSize(new java.awt.Dimension(155, 23));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(155, 100));

        pet_table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Pet", "Pet ID"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        pet_table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pet_tableMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(pet_table);
        pet_table.getColumnModel().getColumn(0).setResizable(false);
        pet_table.getColumnModel().getColumn(1).setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(date_text, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(date_text, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void veterinarian_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_veterinarian_tableMouseClicked
        if(veterinarian_table.getValueAt(veterinarian_table.getSelectedRow(), 0)!=null){
            target="V/"+veterinarian_table.getValueAt(veterinarian_table.getSelectedRow(), 1).toString();            
        }else{
            target=null;
        }
            
            
    }//GEN-LAST:event_veterinarian_tableMouseClicked

    private void owner_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_owner_tableMouseClicked
        if(owner_table.getValueAt(owner_table.getSelectedRow(), 0)!=null){
            target="O/"+owner_table.getValueAt(owner_table.getSelectedRow(), 1).toString();            
        }else{
            target=null;
        }
    }//GEN-LAST:event_owner_tableMouseClicked

    private void pet_tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pet_tableMouseClicked
        if(pet_table.getValueAt(pet_table.getSelectedRow(), 0)!=null){
            target="P/"+pet_table.getValueAt(pet_table.getSelectedRow(), 1);            
        }else{
            target=null;
        }
    }//GEN-LAST:event_pet_tableMouseClicked

    public void repaintOwnerTable(){
        for(int x=0;x<getOwner_table().getRowCount();x++){
            getOwner_table().setValueAt("", x, 0);
            getOwner_table().setValueAt("", x, 1);
        }
        List<Owner> owners = getCo().readAllOwner();
        Iterator it = owners.iterator();
        int row=0;
        while(it.hasNext()){
            Owner ow = (Owner) it.next();
            if(row<getOwner_table().getRowCount()){
                getOwner_table().setValueAt(ow.getName()+" "+ow.getLastName(), row, 0);
                getOwner_table().setValueAt(ow.getId(), row, 1);
            }else{
                DefaultTableModel model = (DefaultTableModel) getOwner_table().getModel();
                model.addRow(new Object[]{ow.getName()+" "+ow.getLastName(),ow.getId()});
            }
            row++;
        }
    }
    
    public void repaintPetTable(){
        for(int x=0;x<getPet_table().getRowCount();x++){
            getPet_table().setValueAt("", x, 0);
            getPet_table().setValueAt("", x, 1);
        }
        List<Pet> pets = getCp().readAllPets();
        Iterator it = pets.iterator();
        int row=0;
        while(it.hasNext()){
            Pet p = (Pet) it.next();
            if(row<getPet_table().getRowCount()){
                getPet_table().setValueAt(p.getName(), row, 0);
                getPet_table().setValueAt(p.getId(), row, 1);
            }else{
                DefaultTableModel model = (DefaultTableModel) getPet_table().getModel();
                model.addRow(new Object[]{p.getName(),p.getId()});
            }
            row++;
        }
    }
    
    public void repaintVeterinarianTable(){
        for(int x=0;x<getVeterinarian_table().getRowCount();x++){
            getVeterinarian_table().setValueAt("", x, 0);
            getVeterinarian_table().setValueAt("", x, 1);
        }
        List<Veterinarian> vets = getCv().readAllVeterinarian();
        Iterator it = vets.iterator();
        int row=0;
        while(it.hasNext()){
            Veterinarian v = (Veterinarian) it.next();
            if(row<getVeterinarian_table().getRowCount()){
                getVeterinarian_table().setValueAt(v.getName(), row, 0);
                getVeterinarian_table().setValueAt(v.getId(), row, 1);
            }else{
                DefaultTableModel model = (DefaultTableModel) getVeterinarian_table().getModel();
                model.addRow(new Object[]{v.getName(),v.getId()});
            }
            row++;
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel date_text;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable owner_table;
    private javax.swing.JTable pet_table;
    private javax.swing.JTable veterinarian_table;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the cp
     */
    public ControlPet getCp() {
        return cp;
    }

    /**
     * @param cp the cp to set
     */
    public void setCp(ControlPet cp) {
        this.cp = cp;
    }

    /**
     * @return the co
     */
    public ControlOwner getCo() {
        return co;
    }

    /**
     * @param co the co to set
     */
    public void setCo(ControlOwner co) {
        this.co = co;
    }

    /**
     * @return the cv
     */
    public ControlVeterinarian getCv() {
        return cv;
    }

    /**
     * @param cv the cv to set
     */
    public void setCv(ControlVeterinarian cv) {
        this.cv = cv;
    }

    /**
     * @return the date_text
     */
    public javax.swing.JLabel getDate_text() {
        return date_text;
    }

    /**
     * @param date_text the date_text to set
     */
    public void setDate_text(javax.swing.JLabel date_text) {
        this.date_text = date_text;
    }

    /**
     * @return the jLabel1
     */
    public javax.swing.JLabel getjLabel1() {
        return jLabel1;
    }

    /**
     * @param jLabel1 the jLabel1 to set
     */
    public void setjLabel1(javax.swing.JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    /**
     * @return the jScrollPane1
     */
    public javax.swing.JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    /**
     * @param jScrollPane1 the jScrollPane1 to set
     */
    public void setjScrollPane1(javax.swing.JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    /**
     * @return the jScrollPane2
     */
    public javax.swing.JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    /**
     * @param jScrollPane2 the jScrollPane2 to set
     */
    public void setjScrollPane2(javax.swing.JScrollPane jScrollPane2) {
        this.jScrollPane2 = jScrollPane2;
    }

    /**
     * @return the jScrollPane3
     */
    public javax.swing.JScrollPane getjScrollPane3() {
        return jScrollPane3;
    }

    /**
     * @param jScrollPane3 the jScrollPane3 to set
     */
    public void setjScrollPane3(javax.swing.JScrollPane jScrollPane3) {
        this.jScrollPane3 = jScrollPane3;
    }

    /**
     * @return the owner_table
     */
    public javax.swing.JTable getOwner_table() {
        return owner_table;
    }

    /**
     * @param owner_table the owner_table to set
     */
    public void setOwner_table(javax.swing.JTable owner_table) {
        this.owner_table = owner_table;
    }

    /**
     * @return the pet_table
     */
    public javax.swing.JTable getPet_table() {
        return pet_table;
    }

    /**
     * @param pet_table the pet_table to set
     */
    public void setPet_table(javax.swing.JTable pet_table) {
        this.pet_table = pet_table;
    }

    /**
     * @return the veterinarian_table
     */
    public javax.swing.JTable getVeterinarian_table() {
        return veterinarian_table;
    }

    /**
     * @param veterinarian_table the veterinarian_table to set
     */
    public void setVeterinarian_table(javax.swing.JTable veterinarian_table) {
        this.veterinarian_table = veterinarian_table;
    }
}
